Introduction
# install packages
install.packages("OpenStreetMap")
install.packages("DT")
install.packages("RColorBrewer")
install.packages("mapproj")
install.packages("sf")
install.packages("RgoogleMaps")
install.packages("scales")
install.packages("rworldmap")
install.packages("maps")
install.packages("tidyverse")
install.packages("rnaturalearth")
install.packages("rnaturalearthdata")
install.packages("rgeos")
install.packages("ggspatial")
install.packages("maptools")
install.packages("leaflet")
install.packages("sf")
install.packages("tmap")
install.packages("here")
install.packages("rgdal")
install.packages("scales")
install.packages("flextable")
# install package from github
devtools::install_github("dkahle/ggmap", ref = "tidyup")
# install klippy for copy-to-clipboard button in code chunks
remotes::install_github("rlesur/klippy")loading packages
# load package
library(OpenStreetMap)
library(DT)
library(RColorBrewer)
library(mapproj)
library(sf)
library(RgoogleMaps)
library(scales)
library(rworldmap)
library(maps)
library(tidyverse)
library(rgeos)
library(ggspatial)
library(maptools)
library(leaflet)
library(sf)
library(tmap)
library(here)
library(rgdal)
library(scales)
library(flextable)
library(ggmap)
library(ggplot2)Generate Maps
พื้นฐานของการ plot geospatial data คือการดาวน์โหลดและพล็อตแผนที่บนพื้นที่ที่ต้องการ ในโปรแกรม R มี packagae หลายตัวที่สามารถใช้ดำเนินการดังกล่าวได้
OpenStreetMapและggmapเป็น package ที่มีแผนที่หลากหลายประเภทให้ผู้วิเคราะห์เลือกใช้ ฟังก์ชันสำหรับดาวน์โหลดแผนที่ของ package ทั้งสองคือopenmap()และggmap()ตามลำดับ
## ggmap
gmap<-get_map(location=c(lon=101.490104, lat=13.03887), zoom=5, scale=2)## Source : https://maps.googleapis.com/maps/api/staticmap?center=13.03887,101.490104&zoom=5&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx
ggmap(gmap)osm <- openmap(upperLeft=c(21,97.6),
lowerRight=c(5.18,106.07),
# type = "osm",
# type = "esri",
type = "stamen-terrain",
minNumTiles=7)## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
plot(osm)Other database
library(rworldmap)
worldmap <- getMap(resolution = "coarse")
plot(worldmap, col = "lightgrey", border = "darkgray",
xlim = c(-180, 180), ylim = c(-90, 90),
bg = "aliceblue",
asp = 1)library(rnaturalearth)
library(rnaturalearthdata)
library(ggspatial)
#load data
world <- ne_countries(scale = "medium", type="countries", returnclass = "sf")
# plot world map
ggplot(data = world) +
geom_sf() +
labs( x = "Longitude", y = "Latitude") +
ggtitle("World map", subtitle = paste0("(", length(unique(world$admin)), " countries)"))ggplot(data = world) +
geom_sf() +
labs( x = "Longitude", y = "Latitude") +
coord_sf(xlim = c(97.6,106.7), ylim = c(5.18,21), expand = T) +
annotation_scale(location = "bl", line_width = 1, width_hint=0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(1.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering) +
theme_bw()Customizing Map
การพล็อตแต่แผนที่นั้นไม่ค่อยมีประโยชน์นัก โดยปกติผู้วิเคราะห์จะพล็อตสารสนเทศอื่น ๆ เป็น layer ซ้อนทับลงบน map ด้วย การพล็อตสารสนเทศเชิงภูมิศาสตร์ลงบนแผนที่สามารถทำได้ 4 ลักษณะได้แก่
points
lines
polygons
raster
ไฟล์ข้อมูล dat61.xlsx เก็บข้อมูลพื้นฐานของโรงเรียนสังหัด สพฐ. ของประเทศไทย
library(readxl)
sch<-read_excel("/Users/siwachoat/Library/Mobile Documents/com~apple~CloudDocs/งานวิจัย/ฐานข้อมูล สทศ./dat61.xlsx")
head(sch)## # A tibble: 6 × 97
## ประเภท ภาค areacode smis `โรงเรียน` กระทรวง `ชื่อโรงเรียน` `ชื่ออำเภอ` `ชื่อจังหวัด`
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 4 3 00100700 10012016 720051 101072… เศรษฐเสถียร เขตดุสิต กรุงเทพมห…
## 2 3 3 00100700 10012029 720111 101072… พิบูลประชาสร… เขตดินแดง กรุงเทพมห…
## 3 4 3 00100700 10012033 720118 101072… โสตศึกษาทุ่งม… เขตสาทร กรุงเทพมห…
## 4 3 3 00100700 12012011 230156 101223… ศึกษาสงเครา… บางกรวย นนทบุรี
## 5 4 3 00100700 12023001 230159 101223… โสตศึกษาจังห… บางบัวทอง นนทบุรี
## 6 4 3 00100700 16012009 490399 101649… ลพบุรีปัญญานุกูล เมืองลพบุรี ลพบุรี
## # … with 88 more variables: ชื่อเขต <chr>, หมู่ <chr>, ชื่อหมู่บ้าน <chr>, ชื่อตำบล <chr>,
## # ไปรษณีย์ <chr>, โทรศัพท์ <chr>, อ.1 ชาย <dbl>, อ.1 หญิง <dbl>, อ.2 ชาย <dbl>,
## # อ.2 หญิง <dbl>, อ.3 ชาย <dbl>, อ.3 หญิง <dbl>, ป.1 ชาย <dbl>, ป.1 หญิง <dbl>,
## # ป.2 ชาย <dbl>, ป.2 หญิง <dbl>, ป.3 ชาย <dbl>, ป.3 หญิง <dbl>, ป.4 ชาย <dbl>,
## # ป.4 หญิง <dbl>, ป.5 ชาย <dbl>, ป.5 หญิง <dbl>, ป.6 ชาย <dbl>, ป.6 หญิง <dbl>,
## # ม.1 ชาย <dbl>, ม.1 หญิง <dbl>, ม.2 ชาย <dbl>, ม.2 หญิง <dbl>, ม.3 ชาย <dbl>,
## # ม.3 หญิง <dbl>, ม.4 ชาย <dbl>, ม.4 หญิง <dbl>, ม.5 ชาย <dbl>, …
ggplot(data = world) +
geom_sf() +
labs( x = "Longitude", y = "Latitude") +
coord_sf(xlim = c(97.6,106.7), ylim = c(5.18,21), expand = T) +
annotation_scale(location = "bl", line_width = 1, width_hint=0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(1.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering) +
theme_bw()+
geom_point(aes(x=long, y=lat), data=sch, col="blue", size=0.1, alpha=0.5)ggplot(data = world) +
geom_sf() +
labs( x = "Longitude", y = "Latitude") +
coord_sf(xlim = c(97.6,106.7), ylim = c(5.18,21), expand = T) +
annotation_scale(location = "bl", line_width = 1, width_hint=0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(1.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering) +
theme_bw()+
geom_point(aes(x=long, y=lat, col=`รวมนักเรียน`/4800, size=`รวมนักเรียน`/48000), data=sch, alpha=0.5, pch=20) scale_color_continuous(trans="reverse")## <ScaleContinuous>
## Range:
## Limits: 0 -- 1
ggplot(data = world) +
geom_sf() +
labs( x = "Longitude", y = "Latitude") +
coord_sf(xlim = c(99.5,101.5), ylim = c(12.5,15), expand = T) +
annotation_scale(location = "bl", line_width = 1, width_hint=0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(1.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering) +
theme_bw()+
geom_point(aes(x=long, y=lat, col=`รวมนักเรียน`/4800, size=`รวมนักเรียน`/48000), data=sch, alpha=0.5, pch=20) scale_color_continuous(trans="reverse")## <ScaleContinuous>
## Range:
## Limits: 0 -- 1